<!--
 * @Descripttion: (产品明细/GB_Production_mx)
 * @Author: (admin)
 * @Date: (2025-06-19)
-->
<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
      <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="15" class="mb10">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['gbproductionmx:add']" plain icon="plus" @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="download" @click="handleExport" v-hasPermi="['gbproductionmx:export']">
          {{ $t('btn.export') }}
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table
      :data="dataList"
      v-loading="loading"
      ref="table"
      border
      header-cell-class-name="el-table-header-cell"
      highlight-current-row
      @sort-change="sortChange"
      >
      <el-table-column prop="xh" label="序号" align="center" v-if="columns.showColumn('xh')"/>
      <el-table-column prop="model" label="机型" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('model')"/>
      <el-table-column prop="bh" label="编号" align="center" v-if="columns.showColumn('bh')"/>
      <el-table-column prop="numOfUnit" label="台数" align="center" v-if="columns.showColumn('numOfUnit')"/>
      <el-table-column prop="unit" label="生产单位" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('unit')"/>
      <el-table-column prop="zt" label="状态" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('zt')"/>
      <el-table-column prop="screw" label="料筒螺杆" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('screw')"/>
      <el-table-column prop="stock" label="储料" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('stock')"/>
      <el-table-column prop="stockMotor" label="储料马达" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('stockMotor')"/>
      <el-table-column prop="hydraulicPump" label="液压泵" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('hydraulicPump')"/>
      <el-table-column prop="motor" label="电动机" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('motor')"/>
      <el-table-column prop="driver" label="驱动器" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('driver')"/>
      <el-table-column prop="controller" label="控制器" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('controller')"/>
      <el-table-column prop="fjzt" label="发机状态" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('fjzt')"/>
      <el-table-column prop="workDate" label="工作日期" :show-overflow-tooltip="true"  v-if="columns.showColumn('workDate')"/>
      <el-table-column label="操作" width="160">
        <template #default="scope">
          <el-button type="primary" size="small" icon="view" title="详情" @click="handlePreview(scope.row)"></el-button>
          <el-button type="success" size="small" icon="edit" title="编辑" v-hasPermi="['gbproductionmx:edit']" @click="handleUpdate(scope.row)"></el-button>
          <el-button type="danger" size="small" icon="delete" title="删除" v-hasPermi="['gbproductionmx:delete']" @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />


    <el-dialog :title="title" :lock-scroll="false" v-model="open" >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
            

          <el-col :lg="12">
            <el-form-item label="机型" prop="model">
              <el-input v-model="form.model" placeholder="请输入机型" />
            </el-form-item>
          </el-col>
            
          <el-col :lg="12">
            <el-form-item label="编号" prop="bh">
              <el-input v-model.number="form.bh" placeholder="请输入编号" />
            </el-form-item>
          </el-col>
            
          <el-col :lg="12">
            <el-form-item label="台数" prop="numOfUnit">
              <el-input v-model.number="form.numOfUnit" placeholder="请输入台数" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="生产单位" prop="unit">
              <el-input v-model="form.unit" placeholder="请输入生产单位" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="状态" prop="zt">
              <el-input v-model="form.zt" placeholder="请输入状态" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="料筒螺杆" prop="screw">
              <el-input v-model="form.screw" placeholder="请输入料筒螺杆" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="储料" prop="stock">
              <el-input v-model="form.stock" placeholder="请输入储料" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="储料马达" prop="stockMotor">
              <el-input v-model="form.stockMotor" placeholder="请输入储料马达" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="液压泵" prop="hydraulicPump">
              <el-input v-model="form.hydraulicPump" placeholder="请输入液压泵" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="电动机" prop="motor">
              <el-input v-model="form.motor" placeholder="请输入电动机" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="驱动器" prop="driver">
              <el-input v-model="form.driver" placeholder="请输入驱动器" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="控制器" prop="controller">
              <el-input v-model="form.controller" placeholder="请输入控制器" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="发机状态" prop="fjzt">
              <el-input v-model="form.fjzt" placeholder="请输入发机状态" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="工作日期" prop="workDate">
              <el-date-picker
                v-model="form.workDate"
                type="datetime"
                placeholder="选择日期时间"
                value-format="YYYY-MM-DD HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" :loading="state.submitLoading" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="gbproductionmx">
import { listGbProductionMx,
 addGbProductionMx, delGbProductionMx, 
 updateGbProductionMx,getGbProductionMx, 
 } 
from '@/api/buss/gbproductionmx.js'
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: 'Bh',
  sortType: 'asc',
})
const columns = ref([
  { visible: true, align: 'center', type: '', prop: 'xh', label: '序号'   },
  { visible: true, align: 'center', type: '', prop: 'model', label: '机型'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'bh', label: '编号'   },
  { visible: true, align: 'center', type: '', prop: 'numOfUnit', label: '台数'   },
  { visible: true, align: 'center', type: '', prop: 'unit', label: '生产单位'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'zt', label: '状态'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'screw', label: '料筒螺杆'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'stock', label: '储料'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'stockMotor', label: '储料马达'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'hydraulicPump', label: '液压泵'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'motor', label: '电动机'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'driver', label: '驱动器'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'controller', label: '控制器'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'fjzt', label: '发机状态'  ,showOverflowTooltip: true  },
  { visible: false, align: 'center', type: '', prop: 'workDate', label: '工作日期'  ,showOverflowTooltip: true  },
  //{ visible: false, prop: 'actions', label: '操作', type: 'slot', width: '160' }
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])


var dictParams = [
]


function getList(){
  loading.value = true
  listGbProductionMx(queryParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery(){
  proxy.resetForm("queryRef")
  handleQuery()
}
// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  single: true,
  multiple: true,
  submitLoading: false,
  form: {},
  rules: {
    xh: [{ required: true, message: "序号不能为空", trigger: "blur"    , type: "number"  }],
  },
  options: {
  }
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel(){
  open.value = false
  reset()
}

// 重置表单
function reset() {
  form.value = {
    xh: null,
    model: null,
    bh: null,
    numOfUnit: null,
    unit: null,
    zt: null,
    screw: null,
    stock: null,
    stockMotor: null,
    hydraulicPump: null,
    motor: null,
    driver: null,
    controller: null,
    fjzt: null,
    workDate: null,
  };
  proxy.resetForm("formRef")
}

/**
 * 查看
 * @param {*} row
 */
function handlePreview(row) {
  reset()
    const id = row.xh
    getGbProductionMx(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = '查看'
      opertype.value = 3
      form.value = {
        ...data,
      }
    }
  })
}

// 添加按钮操作
function handleAdd() {
  reset();
  open.value = true
  state.submitLoading = false
  title.value = '添加产品明细'
  opertype.value = 1
}
// 修改按钮操作
function handleUpdate(row) {
  reset()
  const id = row.xh || ids.value
  getGbProductionMx(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = '修改产品明细'
      opertype.value = 2

      form.value = {
        ...data,
      }
    }
  })
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      state.submitLoading = true

      if (form.value.xh != undefined && opertype.value === 2) {
        updateGbProductionMx(form.value).then((res) => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addGbProductionMx(form.value).then((res) => {
            proxy.$modal.msgSuccess("新增成功")
            open.value = false
            getList()
          })
          .finally(() => {
            setTimeout(() => {
              state.submitLoading = false
            }, 800)
          })
      }
    }
  })
}

// 删除按钮操作
function handleDelete(row) {
  const Ids = row.xh || ids.value

  proxy
    .$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？', "警告", {
      confirmButtonText: proxy.$t('common.ok'),
      cancelButtonText: proxy.$t('common.cancel'),
      type: "warning",
    })
    .then(function () {
      return delGbProductionMx(Ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    })
}



// 导出按钮操作
function handleExport() {
  proxy
    .$confirm("是否确认导出产品明细数据项?", "警告", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
    .then(async () => {
      await proxy.downFile('/Buss/GbProductionMx/export', { ...queryParams })
    })
}

handleQuery()
</script>